import Storage from '../../utils/Storage'
import CacheMethod from "../../utils/CacheMethod";
import { getConfig } from '../../config'

export default (url) => {
    if (/feecee.cn/.test(url) || /xaivan.com/.test(url)) {
        let token = Storage.get(getConfig().TokenKey)
        let openId = Storage.get('openId')
        if (/need_login=1/.test(url)) {
            if (!token) {
                CacheMethod.backRouter = ({ token, openId }) => {
                    next('redirectTo', url, token, openId)
                }
                uni['navigateTo']({
                    url: `/pages/login/index`
                })
                return
            }

            next('navigateTo', url, token, openId)
        } else if (/need_login=2/.test(url)) {
            next('navigateTo', url, token, openId)
        } else {
            next('navigateTo', url)
        }
    } else {
        next('navigateTo', url)
    }
}

const next = (type, url, token = '', openId = '') => {
    let ts = token ? `&token=${token}` : ''
    let os = openId ? `&openId=${openId}` : ''
    url += `${/\?/.test(url) ? '&' : '?'}X-Auth-Appid=${getConfig().Appid}${ts}${os}`
    uni[type]({
        url: `${`/pages/webview/index?link=`}${encodeURIComponent(url)}`
    })
}